home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2919 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. Path: info.spt.net.cn!usenet
  2. From: txwang@public.sta.net.cn (Wang TianXing)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question about destructor...
  5. Date: Sat, 20 Jan 1996 14:50:28 GMT
  6. Organization: No Organization
  7. Message-ID: <4dqv04$nua@info.sta.net.cn>
  8. References: <4ce9mk$atg@eng_ser1.erg.cuhk.hk> <4cqrei$par@isoit109.bbn.hp.com> <4dkahd$lv9@alibaba.kmit.sk>
  9. NNTP-Posting-Host: ts2-3.sta.net.cn
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. brano@alibaba (Brano Zahradnik) wrote:
  13.  
  14. | Manfred_Lange (Manfred_Lange@bbn.hp.com) wrote:
  15. | : ywleung@cs.cuhk.hk (Marty McFly) wrote:
  16. | : >Dear All,
  17. | : >
  18. | : >    I now have 2 classes A and B.  B is a derived class of A.  However,
  19. | : >the internal implementation of A is not known, i.e., the private members of
  20. | : >class A are not provided, only the public members are given.  But class B is 
  21. | : >implemented by myself.  So what should be written in the destructor of B so 
  22. | : >that all the private members inherited from A are also "deleted"?
  23.  
  24. | : I saw all the answers you got for your question. It is true, that you don't have 
  25. | : to care for the destructor in the baseclass.
  26.  
  27. | : But there is another aspect of the problem, that is important to know. Take the 
  28. | : following sample:
  29.  
  30. | : class Base
  31. | : {
  32. | :    ~Base();
  33. | : };
  34.  
  35. | : class Derived : public Base
  36. | : {
  37. | :    ~Derived();
  38. | : };
  39.  
  40. | : ..
  41.  
  42. | : {  Base*   aBase;
  43.  
  44. | :    aBase = new Derived();
  45.  
  46. | :    ...
  47.  
  48. | :    delete aBase;
  49.  
  50. | : ..
  51.  
  52.  
  53. | : Which destructor will be called when executing "delete aBase()" ?
  54.  
  55. | I think, that destructors are calling in reverse order than constructors.
  56. | In this example: ~Derived,~Base
  57. NO. Only ~Base() will be called. So, that's a problem.
  58.  
  59. If you do know aBase is pointing to a Derived, you can:
  60.  
  61.     delete (Derived *)aBase;
  62.  
  63. If not, you have to declare ~Base() as virtual, but this requires that
  64. you have the source code of the Base class.
  65.  
  66. | Q: Why you need rewrite destructor of base ??
  67.  
  68. See above. (BTW, I don't think the original poster realized that he
  69. have to do this.)
  70.  
  71. ---
  72. Wang TianXing
  73. txwang@public.sta.net.cn
  74.  
  75.  
  76.